package com.yyge.util.sms;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.google.gson.Gson;
import com.yyge.common.enums.AckCode;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.yyge.common.context.AliyunContext;
import com.yyge.common.exception.ServiceException;
//import com.tencentcloudapi.common.Credential;
//import com.tencentcloudapi.common.exception.TencentCloudSDKException;
//import com.tencentcloudapi.sms.v20210111.SmsClient;
//import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
//import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
//import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.log4j.Log4j2;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author gityyge
 * @Description 模板方法
 * @Date 2023/7/11 15:45
 * @Version 1.0
 */
@Log4j2
public class SmsTemplate {

    private final String keyid="LTAI5tB8JuaUsvUnxHThAw4S";
    private final String keySecret="JEdmcGALQeMJS44yS57wmKKeO8MwRY";

    /**
     * 第1步，创建认证账号
     * 查看地址 https://console.cloud.tencent.com/cam/capi
     */
    private StaticCredentialProvider createCredential(){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
          .accessKeyId(keyid)
          .accessKeySecret(keySecret)
          .build());
        return provider;
    }


    /**
     * 设置client模版
     */
     private AsyncClient getClient(StaticCredentialProvider provider){
        AsyncClient client= AsyncClient.builder()
                .region("cn-chongqing") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                        .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        return client;
    }



    /**
     * 创建模版-发短信
     */
    public  void sendSms(SmsRequestReq req) throws ExecutionException, InterruptedException {
        AsyncClient client=null;
        try {
            StaticCredentialProvider provider = createCredential();
            client=getClient(provider);

            // Parameter settings for API request
            System.out.println("手机号码:"+req.getPhone()[0]);
            System.out.println("signName:"+req.getTemplateName());
            System.out.println("templateCode:"+req.getTemplateId());
            String jsonOn = new Gson().toJson(req.paramSet);
            System.out.println("改造之后..........................");
            System.out.println(jsonOn);
            String json = "{\"code\":\""+req.params()[0]+"\"}";

            System.out.println("templateParam:"+json);

            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName(req.getTemplateName())
                    .templateCode(req.getTemplateId())

                    .phoneNumbers(req.getPhone()[0])
                    .templateParam(jsonOn)
                    .build();



            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            System.out.println("打印一下看看-----------------------------------------");
            System.out.println(new Gson().toJson(resp));
        } finally {
            client.close();
        }
    }
}